﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace System.Controls.Converters
{
  public class EnumMatchToBooleanConverter : IValueConverter
  {
    public object Convert( object value, Type targetType, object parameter, CultureInfo culture )
    {
      if ( !Enum.IsDefined( value.GetType(), value ) )
        return DependencyProperty.UnsetValue;

      var parameterString = parameter as string;
      if ( parameterString == null )
        return DependencyProperty.UnsetValue;

      var parameterValue = Enum.Parse( value.GetType(), parameterString );

      return parameterValue.Equals( value );
    }

    public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture )
    {
      var parameterString = parameter as string;
      if ( parameterString == null )
        return DependencyProperty.UnsetValue;

      return Enum.Parse( targetType, parameterString );
    }
  }
}
